﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Net;
using System.IO;
using Microsoft.WindowsMobile.Samples.Location;

namespace CommunityMegaphoneReader
{
    public static class GpsHelper
    {
        public static DataSet LoadRss(string requestUriString)
        {
            HttpWebRequest feed = HttpWebRequest.Create(requestUriString) as HttpWebRequest;
            StreamReader streamReader = new StreamReader(feed.GetResponse().GetResponseStream());

            string rssXml = streamReader.ReadToEnd();
            rssXml = rssXml.Replace(@"<?xml version=""1.0"" encoding=""utf-8""?>", string.Empty);

            StringReader stringReader = new StringReader(rssXml);

            DataSet dataSet = new DataSet();
            dataSet.ReadXml(stringReader);

            return dataSet;
        }

        public static List<Item> GetEvents(DataSet dataSet, GpsPosition gpsPosition)
        {
            List<Item> items = new List<Item>();

            foreach (DataRow dataRow in dataSet.Tables["item"].Rows)
            {
                Item item = new Item();
                item.Title = (string)dataRow["title"];
                item.Description = (string)dataRow["description"];
                item.Link = (string)dataRow["link"];
                item.PublishDate = DateTime.Parse((string)dataRow["pubDate"]);
                item.Latitude = Double.Parse((string)dataRow["lat"]);
                item.Longitude = Double.Parse((string)dataRow["long"]);

                if(gpsPosition.LatitudeValid && gpsPosition.LongitudeValid)
                    item.Distance = GeoCodeCalc.CalcDistance(gpsPosition.Latitude, gpsPosition.Longitude, item.Latitude, item.Longitude);

                items.Add(item);
            }

            items.Sort(delegate(Item item1, Item item2)
            {
                return item1.Distance.CompareTo(item2.Distance);
            });

            return items;
        }

        public static string DisplayEvents(List<Item> events)
        {
            StringBuilder stringBuilder = new StringBuilder();

            foreach (Item item in events)
            {
                stringBuilder.Append(@"<span style=""color: #FFFFFF;font-weight:bold;"">");
                stringBuilder.AppendFormat(@"<a href=""{0}"" style=""color: #DBB94F;"">{1}</a><br/><br/>", item.Link, item.Title);
                stringBuilder.AppendFormat(@"{0}<br/><br/>", item.Description);
                stringBuilder.AppendFormat(@"<b>Distance: <span style=""color: #DBB94F;"">{0} miles</span></b><br/>", item.Distance.ToString("0.00"));
                // stringBuilder.AppendFormat(@"<b>{0}</b><br/>", item.PublishDate);
                // stringBuilder.AppendFormat(@"{0}<br/>", item.Latitude);
                // stringBuilder.AppendFormat(@"{0}<br/>", item.Longitude);
                stringBuilder.AppendFormat(@"<br/><br/>");
                stringBuilder.Append(@"</span>");
            }

            return stringBuilder.ToString();
        }
    }
}
